package sort;


import validation.Isnumeric;


public class BufferSorting 
{
					 
	String split[],strValue[];
	private int index;
	int intValue[];
	
	
	public String[] bufferSorting(String buffer[],String str)
	{
	
		String[] splitStr=buffer[0].split("\t");
		split=new String[splitStr.length];
		strValue=new String[buffer.length-1];
		//Isnumeric isn=new Isnumeric();
		intValue = new int[buffer.length-1];
		split=buffer[0].split("\t");
		for(int i=0;i<split.length;i++)
		{			
			if(split[i].toLowerCase().equals(str.toLowerCase()))
			{
				index=i;
				//System.out.println("index1 is "+index1);
			}
		}
		
		for(int i=1;i<buffer.length;i++)
		{
			split=buffer[i].split("\t");		
			strValue[i-1]=split[index];
			//System.out.println(strValue[i-1]);
		}
		//for(int i =0;i<split.length;i++)
		//	System.out.println(strValue[i]);
		for(int i=0;i<buffer.length-1;i++)
		{
			//if(isn.validate(strValue[i])==true)
			{
			//intValue[i]=Integer.parseInt(strValue[i]);
		 	//System.out.print(intValue[i]+" ");
			}
			//else
			{
				strValue[i]=strValue[i];
				//System.out.println(strValue[i]);
			}
			
		}
	

		

		if(intValue[0]!=0)
		{
			sortIds(intValue);
			String intStr[]=new String[intValue.length];
			for (int j = 0; j < buffer.length-1; j++)
			{
				Integer i=new Integer(intValue[j]);
					intStr[j]=i.toString();
				//System.out.print(intValue[j] + " ");
			}
			return intStr;			
		}
		
		else
		{
			sortStrings(strValue,strValue.length);
			return strValue;
			//for(int x = 0; x < buffer.length-1; ++x)
			//	System.out.print(strValue[x]+" ");	
		}	 
	
	
}


	void sortIds(int a[])
	{
		for (int i = a.length; --i>=0; )
		{		           
		    for (int j = 0; j<i; j++)
		    {
		    	if (a[j] > a[j+1]) 
		    	{
		    		int T = a[j];
		    		a[j] = a[j+1];
		    		a[j+1] = T;
			   }
		    }			
		}   
		//for(int x = 0; x < a.length; ++x)
					//System.out.print(a[x]+" ");
		
	}

	 void sortStrings(String[] array, int len)
	{		
	int a, b;
	String temp;
	int highSubscript = len - 1;
	for(a = 0; a < highSubscript; ++a)
		for(b = 0; b < highSubscript; ++b)
			if(array[b].toLowerCase().compareTo(array[b + 1].toLowerCase()) > 0)
			{
				temp = array[b];
				array[b] = array[b + 1];
				array[b + 1] = temp;
			}	
	}		
		
		
	
	
	public static void main(String args[])
	{			
		String buffer[]=new String[]{"id\tName\tAddr\tDesig\tPhone\tCas",
									 "1\tshweta\tpune\tsw\t111201\tH",
									 "2\tmegha\tbanglore\thr\t101301\tH",
									 "3\tReena\tDelhi\thw\t101\tH",
									 "4\theena\tpatna\tst\t1123\tG",
									 "5\tvrinda\tkolkata\tst\t10\tG",
									 "6\tkomal\traipur\thw\t11223\tH"
				 					};
			String str="phone";
		
			BufferSorting bfs=new BufferSorting();
			buffer=bfs.bufferSorting(buffer,str);
			for(int x = 0; x < buffer.length; ++x)
					System.out.print(buffer[x]+" ");	
			
	}
}
